import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col, Card} from 'antd';
import {FORM_OPTIONS, FORM_ROW_OPTIONS} from '../constants';
import {PERMISSION} from './constants';
import {kDClaimType, claimCompensationWay, urgencyLevel, kDClaimStatus} from '../Enum';
import WrappedSelect from '../common/WrappedSelect';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import DateRangePicker from 'Shared/components/DateRangePicker';
import TagSelect from 'Shared/components/TagSelect';
import TextInput from 'Shared/components/TextInput';
import Export from '../common/Export';
import {injectIntl, language} from './intl';
import routes from './routes';
class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    onSelectDealer = data => {
        this.handleFilterChange(data.id, 'dealerId');
        this.props.onSelect({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {brands} = this.props.initEnumData;
        const {isDealerUser, conditions, queryDealer} = this.props;
        const dealerInfo = conditions.dealerId ? {
            id: queryDealer.id,
            code: queryDealer.code,
            name: queryDealer.name
        } : null;
        return (
            <div>
                <Form className="form-standard">
                    <Card>
                        <Row>
                            <Col {...FORM_OPTIONS.col}>
                                <Form.Item
                                    label={formatMessage({
                                        id: 'queryPanel.form.code',
                                        defaultMessage: '申请单号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="code"
                                        value={conditions.code}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onBlur={this.handleFilterChange}/>
                                </Form.Item>
                            </Col>
                            {!isDealerUser && <Col {...FORM_OPTIONS.col}>
                                <Form.Item
                                    label={formatMessage({
                                        id: 'queryPanel.form.dealerId',
                                        defaultMessage: '总代'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <DealerSelectPanel
                                        value={dealerInfo}
                                        onSelect={this.onSelectDealer} />
                                </Form.Item>
                            </Col>}
                            {brands.length > 1 && <Col {...FORM_OPTIONS.col}>
                                <Form.Item label={formatMessage({
                                    id: 'queryPanel.form.brandId',
                                    defaultMessage: '品牌'
                                })}{...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="brandId"
                                        value={conditions.brandId}
                                        options={brands}
                                        onChange={this.handleFilterChange}/>
                                </Form.Item>
                            </Col>}
                            <Col {...FORM_OPTIONS.col}>
                                <Form.Item
                                    label={formatMessage({
                                        id: 'queryPanel.form.claimType',
                                        defaultMessage: '索赔类型'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="claimType"
                                        value={conditions.claimType}
                                        options={kDClaimType.toList(language)}
                                        onChange={this.handleFilterChange} />
                                </Form.Item>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <Form.Item
                                    label={formatMessage({
                                        id: 'queryPanel.form.actualCompensationWay',
                                        defaultMessage: '实际赔付方式'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="actualCompensationWay"
                                        value={conditions.actualCompensationWay}
                                        options={claimCompensationWay.toList(language)}
                                        onChange={this.handleFilterChange} />
                                </Form.Item>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <Form.Item
                                    label={formatMessage({
                                        id: 'queryPanel.form.saleShipmentCode',
                                        defaultMessage: '发运单编号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="saleShipmentCode"
                                        value={conditions.saleShipmentCode}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onBlur={this.handleFilterChange}/>
                                </Form.Item>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <Form.Item
                                    label={formatMessage({
                                        id: 'queryPanel.form.batchNumber',
                                        defaultMessage: '批次号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="batchNumber"
                                        value={conditions.batchNumber}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onBlur={this.handleFilterChange}/>
                                </Form.Item>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <Form.Item
                                    label={formatMessage({
                                        id: 'queryPanel.form.urgencyLevel',
                                        defaultMessage: '紧急程度'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="urgencyLevel"
                                        value={conditions.urgencyLevel}
                                        options={urgencyLevel.toList(language)}
                                        onChange={this.handleFilterChange} />
                                </Form.Item>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <Form.Item
                                    label={formatMessage({
                                        id: 'queryPanel.form.kdBomCode',
                                        defaultMessage: 'KDBOM号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="kdBomCode"
                                        value={conditions.kdBomCode}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onBlur={this.handleFilterChange}/>
                                </Form.Item>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <Form.Item
                                    label={formatMessage({
                                        id: 'queryPanel.form.shipPartCode',
                                        defaultMessage: '发运零件号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="shipPartCode"
                                        value={conditions.shipPartCode}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onBlur={this.handleFilterChange}/>
                                </Form.Item>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <Form.Item
                                    label={formatMessage({
                                        id: 'queryPanel.form.createTime',
                                        defaultMessage: '申请日期'
                                    })}{...FORM_OPTIONS.item}>
                                    <DateRangePicker
                                        name="createTime"
                                        value={conditions.createTime}
                                        onChange={this.handleFilterChange} />
                                </Form.Item>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <Form.Item
                                    label={formatMessage({
                                        id: 'queryPanel.form.firstApproveTime',
                                        defaultMessage: '初审日期'
                                    })}{...FORM_OPTIONS.item}>
                                    <DateRangePicker
                                        name="firstApproveTime"
                                        value={conditions.firstApproveTime}
                                        onChange={this.handleFilterChange} />
                                </Form.Item>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <Form.Item
                                    label={formatMessage({
                                        id: 'queryPanel.form.approveTime',
                                        defaultMessage: '复审日期'
                                    })}{...FORM_OPTIONS.item}>
                                    <DateRangePicker
                                        name="approveTime"
                                        value={conditions.approveTime}
                                        onChange={this.handleFilterChange} />
                                </Form.Item>
                            </Col>
                        </Row>
                        <Row>
                            <Col>
                                <Form.Item label={formatMessage({
                                    id: 'queryPanel.form.status',
                                    defaultMessage: '状态'
                                })} {...FORM_ROW_OPTIONS.item}>
                                    <TagSelect
                                        name="status"
                                        value={conditions.status}
                                        options={kDClaimStatus.toList(language)}
                                        onChange={this.handleFilterChange} />
                                </Form.Item>
                            </Col>
                        </Row>
                        <Row>
                            <Col span={6}>
                                <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>{formatMessage({
                                    id: 'queryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}</Button>
                                <Button key="clear" onClick={this.props.onResetClick}>{formatMessage({
                                    id: 'queryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}</Button>
                            </Col>
                            <Col span={18} className="col-align-right">
                                {this.props.exportable && <Export key={PERMISSION.export} exportRequest={this.props.onClickExport} />}
                                {this.props.addable && <Button key="add" type="primary" href={`#${routes.add.url()}`}>{formatMessage({
                                    id: 'queryPanel.btn.add',
                                    defaultMessage: '新增'
                                })}</Button>}
                            </Col>
                        </Row>
                    </Card>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
};

import {connect} from 'react-redux';
import {saveQueryCondition, querySearch, resetQueryPanel, saveQueryDealer, exportData} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';

const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: item.name,
            value: item.id
        })) : [];
        return {
            brands
        };
    }
);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getQueryDealer = selectorFactory(['page', 'appState', 'queryDealer']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    initEnumData: getinitEnumData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    exportable: state.getIn(['page', 'domainData', 'permission', 'data']).includes(PERMISSION.export),
    addable: state.getIn(['page', 'domainData', 'permission', 'data']).includes(PERMISSION.add),
    queryDealer: getQueryDealer(state),
    isDealerUser: state.getIn((['page', 'domainData', 'init', 'isDealerUser'])),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
    onSelect: data => dispatch(saveQueryDealer(data)),
    onClickExport: () => dispatch(exportData())
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
